
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "user_uart_serial.h"
#include "user_fushi_drive.h"

#define USER_RECVBUFF_LEN (128)
#define USER_SEND_INV_TIMES (600)//s
static uint16_t user_send_fushi_times_num_1000ms=0;



const uint8_t user_send1[8]={0x63,0x04,0x00,0x00,0x00,0x22,0x78,0x51};//1个设备到银保
const uint8_t user_send2[8]={0x63,0x04,0x00,0x00,0x00,0x34,0xF9,0x9F};//2个设备到银保
const uint8_t user_send_fushi[8]={0x63,0x04,0x00,0x00,0x00,0x16,0x79,0x86};//1个设备只有腐蚀

const uint8_t user_send_read_pro_flag[8]={0x63,0x01,0x00,0x00,0x00,0x01,0xF5,0x88};
const uint8_t user_send_pro_open[8]={0x63,0x05,0x00,0x00,0xFF,0x00,0x84,0x78};
const uint8_t user_send_pro_close[8]={0x63,0x05,0x00,0x00,0x00,0x00,0xC5,0x88};

const uint8_t user_send_read_pro_device1[8] ={0x63,0x01,0x00,0x01,0x00,0x01,0xA4,0x48};
const uint8_t user_send_pro_device1_open[8]={0x63,0x05,0x00,0x01,0xFF,0x00,0xD5,0xB8};
const uint8_t user_send_pro_device1_close[8]={0x63,0x05,0x00,0x01,0x00,0x00,0x94,0x48};

const uint8_t user_send_read_pro_device2[8] ={0x63,0x01,0x00,0x02,0x00,0x01,0x54,0x48};
const uint8_t user_send_pro_device2_open[8]={0x63,0x05,0x00,0x02,0xFF,0x00,0x25,0xB8};
const uint8_t user_send_pro_device2_close[8]={0x63,0x05,0x00,0x02,0x00,0x00,0x64,0x48};

const uint8_t user_send_reset[5]={0x63,0x46,0x01,0x72,0x7E};
const uint8_t user_send_read_device_number[8]={0x63,0x03,0x00,0x02,0x00,0x01,0x2D,0x88};

/*
send_status=0 send read data
			1 read device number
			2 read prodect status
			3 set prodect open 
			4 set prodect close 
			5 set device1 open prodect
			6 set device1 close prodect
			7 set device2 open prodect
			8 set device3 close prodect
			9 set reset
			10 set check time 63 06 00 00 00 3C 81 99
			11 set device time 63 43 10 07 20 23 04 19 21 48 00 3F 5B
*/
typedef struct
{
	uint8_t device_number;
	uint8_t prodect_status;
	uint8_t send_status;
	uint16_t check_time;
	uint8_t device_time[7];

}USER_FUSHI_STATUS_T;




static USER_FUSHI_STATUS_T user_fushi_status;

static USER_UART_SERIAL_T user_drive_serial_handle;

static uint8_t user_recvbuff[USER_RECVBUFF_LEN];
static uint8_t user_recvbuff_tmp[USER_RECVBUFF_LEN];

static uint8_t user_send_times=0;
/*********************************************/
#define one_day_sec (1440)
static uint32_t day_conunt_number=0;

void user_fushi_drive_reset(void)
{
	day_conunt_number++;
	if(day_conunt_number>720)
	{
		if(user_fushi_status.send_status==FUSHI_READ_DATA)
		{
			user_fushi_status.send_status=FUSHI_SET_DEVICE_RESET;
			day_conunt_number=0;
		}
	} 
}

void user_fushi_drive_init(void)
{
	memset(user_recvbuff,0, sizeof(user_recvbuff));
	
	user_drive_serial_handle.user_recvstart=0;
	user_drive_serial_handle.user_recvend=0;
	user_drive_serial_handle.user_recvbuff=user_recvbuff;
	user_drive_serial_handle.user_recvbuff_len=sizeof(user_recvbuff);
	user_uart_serial_init(&user_drive_serial_handle);
	user_fushi_status.device_number=0;
	user_fushi_status.prodect_status=0;
	user_fushi_status.send_status=1;
	user_fushi_status.check_time=0;
	memset(user_fushi_status.device_time,0, sizeof(user_fushi_status.device_time));
	
}
void user_fushi_set_fushi_status(uint8_t send_status,uint16_t cehck_time,uint8_t *device_time)
{
	uint8_t i;
	user_fushi_status.check_time=cehck_time;
	user_fushi_status.send_status=send_status;
	if(device_time!=NULL)
	{
		memset(user_fushi_status.device_time,0,7);
		memcpy(user_fushi_status.device_time,device_time,7);
		printf("user_fushi_set_fushi_status\r\n");
		for(i=0;i<7;i++)
		{
			printf(" %x",user_fushi_status.device_time[i]);
		}
	}
	
	
}
void user_fushi_drive_send(UART_HandleTypeDef *huart,USER_CENTRAL_CONTROL_STATUS_T *status)
{
	uint16_t crc_tmp=0;
	uint8_t time_set_data[13]={0x63,0x43,0x10,0x07,0x20,0x23,0x04,0x19,0x21,0x48,0x00,0x3F,0x5B};
	uint8_t check_set_data[8]={0x63,0x06,0x00,0x00,0x00,0x3C,0x81,0x99};
	uint8_t i;
	user_fushi_drive_reset();
	switch(user_fushi_status.send_status)
	{
		case FUSHI_READ_DATA://读数据
			if(user_fushi_status.device_number==1)
			{
				if(user_fushi_status.prodect_status==0)
				{
				printf("user_fushi_drive_send\r\n");
					HAL_UART_Transmit(huart , (uint8_t *)user_send_fushi, sizeof(user_send_fushi), 2000);
				}
				else
				{
					HAL_UART_Transmit(huart , (uint8_t *)user_send1, sizeof(user_send1), 2000);
				}
			}
			else if(user_fushi_status.device_number==2)
			{
				if(user_fushi_status.prodect_status==0)
				{
					//HAL_UART_Transmit(huart , (uint8_t *)user_send_fushi, sizeof(user_send_fushi), 2000);
				}
				else
				{
					HAL_UART_Transmit(huart , (uint8_t *)user_send2, sizeof(user_send2), 2000);
				}
			}
			else
			{
				user_fushi_status.send_status=1;
			}
		break;
		case FUSHI_READ_DEVICE_NUMBER://读传感器数
			HAL_UART_Transmit(huart , (uint8_t *)user_send_read_device_number, sizeof(user_send_read_device_number), 2000);
		break;
		case FUSHI_READ_PRO://读阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_read_pro_flag, sizeof(user_send_read_pro_flag), 2000);
		break;
		case FUSHI_SET_POR_OPEN://开阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_pro_open, sizeof(user_send_pro_open), 2000);
		break;
		case FUSHI_SET_POR_CLOSE://关阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_pro_close, sizeof(user_send_pro_close), 2000);
		break;
		case FUSHI_READ_DEVICE1:
			HAL_UART_Transmit(huart , (uint8_t *)user_send_read_pro_device1, sizeof(user_send_read_pro_device1), 2000);
		break;
		case FUSHI_SET_DEVICE1_OPEN://开设备1阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_pro_device1_open, sizeof(user_send_pro_device1_open), 2000);
		break;
		case FUSHI_SET_DEVICE1_CLOSE://关设备1阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_pro_device1_close, sizeof(user_send_pro_device1_close), 2000);
		break;
		case FUSHI_READ_DEVICE2:
			HAL_UART_Transmit(huart , (uint8_t *)user_send_read_pro_device2, sizeof(user_send_read_pro_device2), 2000);
		break;
		case FUSHI_SET_DEVICE2_OPEN://开设备2阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_pro_device2_open, sizeof(user_send_pro_device2_open), 2000);
		break;
		case FUSHI_SET_DEVICE2_CLOSE://关设备2阴保
			HAL_UART_Transmit(huart , (uint8_t *)user_send_pro_device2_close, sizeof(user_send_pro_device2_close), 2000);
		break;
		case FUSHI_SET_DEVICE_RESET://重置
			HAL_UART_Transmit(huart , (uint8_t *)user_send_reset, sizeof(user_send_reset), 2000);
		break;
		case FUSHI_SET_DEVICE_TIME:
			time_set_data[4]=user_fushi_status.device_time[0];
			time_set_data[5]=user_fushi_status.device_time[1];
			time_set_data[6]=user_fushi_status.device_time[2];
			time_set_data[7]=user_fushi_status.device_time[3];
			time_set_data[8]=user_fushi_status.device_time[4];
			time_set_data[9]=user_fushi_status.device_time[5];
			time_set_data[10]=user_fushi_status.device_time[6];
			
			crc_tmp=CRC16 ((const uint8_t*)time_set_data, 11);
			time_set_data[11]=crc_tmp%256;
			time_set_data[12]=crc_tmp/256;
			for(i=0;i<sizeof(time_set_data);i++)
			{
				printf(" %x",time_set_data[i]);
			}
			printf("\r\n");
			HAL_UART_Transmit(huart , (uint8_t *)time_set_data, sizeof(time_set_data), 2000);
		break;
		case FUSHI_SET_CHECK_TIME:
			printf("FUSHI_SET_CHECK_TIME\r\n");
			check_set_data[4]=user_fushi_status.check_time/256;
			check_set_data[5]=user_fushi_status.check_time%256;
			crc_tmp=CRC16 ((const uint8_t*)check_set_data, 6);
			check_set_data[6]=crc_tmp%256;
			check_set_data[7]=crc_tmp/256;
			for(i=0;i<sizeof(check_set_data);i++)
			{
				printf(" %x",check_set_data[i]);
			}
			printf("\r\n");
			HAL_UART_Transmit(huart , (uint8_t *)check_set_data, sizeof(check_set_data), 2000);

		break;
	}
}

void user_fushi_drive_add_data(uint8_t data)
{
	user_uart_serial_add_data(&user_drive_serial_handle,data);
}

uint8_t user_fushi_drive_handle(void)
{
	uint16_t data_recv_len_tmp=0;
	uint16_t crc_tmp=0;
	uint8_t i;
	USER_CENTRAL_CONTROL_STATUS_T*  tmp=user_central_control_get_status();
	printf("user_fushi_drive_handle send_status is %d\r\n",user_fushi_status.send_status);
	switch(user_fushi_status.send_status)
	{
		case FUSHI_READ_DATA:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=5)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				do
				{
					data_recv_len_tmp=0;
					data_recv_len_tmp=user_uart_serial_check_data(&user_drive_serial_handle,user_recvbuff_tmp,1);
					if(user_recvbuff_tmp[0]!=0x63)
					{
						user_uart_serial_end_add1(&user_drive_serial_handle);
					}
					else
					{
						break;
					}
				}while(data_recv_len_tmp);
				
				
				data_recv_len_tmp=0;
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				data_recv_len_tmp=user_uart_serial_check_data(&user_drive_serial_handle,user_recvbuff_tmp,3);
				if(data_recv_len_tmp==3)
				{						
					
					if(user_recvbuff_tmp[1]==0x84)
					{
						memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
						user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,5);
						crc_tmp=CRC16 ((const uint8_t*)user_recvbuff_tmp, 3);
						//printf("crc_tmp  is %x\r\n",crc_tmp);
						if((user_recvbuff_tmp[4]==crc_tmp/256)&&(user_recvbuff_tmp[3]==crc_tmp%256))
						{
							printf("0x84 crc is ok %x\r\n",user_recvbuff_tmp[2]);
							if(user_recvbuff_tmp[2]==0x02)//address error
							{
								user_send_times=0;
								return 1;
							}
							else
							{
								user_send_times=0;
								return 0;
							}
						}
					}
					else if(user_recvbuff_tmp[1]==0x04)
					{
						
						if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=user_recvbuff_tmp[2]+5)
						{
							memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
							user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
							crc_tmp=CRC16 ((const uint8_t*)user_recvbuff_tmp, user_recvbuff_tmp[2]+3);
			
							if((user_recvbuff_tmp[ user_recvbuff_tmp[2]+4]==crc_tmp/256)&&(user_recvbuff_tmp[user_recvbuff_tmp[2]+3]==crc_tmp%256))
							{
								printf("0x04 crc is ok\r\n");
								
								memcpy(tmp->central_control_payloadbuff+5,user_recvbuff_tmp+3,user_recvbuff_tmp[2]);//payload 覆盖 tmp
								tmp->central_control_payloadbuff_len=user_recvbuff_tmp[2];
								printf("data is \r\n");
								for(i=0;i<user_recvbuff_tmp[2];i++)
								{
									printf(" %x",tmp->central_control_payloadbuff[5+i]);
								}
								printf("\r\n");
								user_send_times=0;
								return 0;
								
							}
						}
					}
					else
					{
						user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,2);
					}
				}
			}
		break;
		case FUSHI_READ_DEVICE_NUMBER:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=7)
			{
				
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				crc_tmp=CRC16 ((const uint8_t*)user_recvbuff_tmp, 5);
				if((user_recvbuff_tmp[6]==crc_tmp/256)&&(user_recvbuff_tmp[5]==crc_tmp%256))
				{
					for(i=0;i<7;i++)
					{
						printf(" %x",user_recvbuff_tmp[i]);
					}
					printf("\r\n");
					user_send_times=0;
					user_fushi_status.device_number=user_recvbuff_tmp[3]*256+user_recvbuff_tmp[4];
					printf("device_number is %d \r\n",user_fushi_status.device_number);
					user_fushi_status.send_status=FUSHI_READ_PRO;
					return 0;
				}
				
			}
		break;
		case FUSHI_READ_PRO:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=6)
			{
				
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				crc_tmp=CRC16 ((const uint8_t*)user_recvbuff_tmp, 4);
				if((user_recvbuff_tmp[5]==crc_tmp/256)&&(user_recvbuff_tmp[4]==crc_tmp%256))
				{
					for(i=0;i<6;i++)
					{
						printf(" %x",user_recvbuff_tmp[i]);
					}
					printf("\r\n");
					user_send_times=0;
					user_fushi_status.prodect_status=user_recvbuff_tmp[3];
					printf("prodect_status is %d \r\n",user_fushi_status.prodect_status);
					user_fushi_status.send_status=FUSHI_READ_DEVICE1;//read data
					return 0;
					
				}
				
			}
		break;
		case FUSHI_SET_POR_OPEN:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DEVICE_NUMBER;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_POR_CLOSE:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DEVICE_NUMBER;//read data
				user_send_times=0;
				return 0;
			}
		break;
			
		case FUSHI_READ_DEVICE1:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=6)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				printf("FUSHI_READ_DEVICE1 is %d \r\n",user_recvbuff_tmp[3]);
				user_fushi_status.send_status=FUSHI_READ_DEVICE2;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_DEVICE1_OPEN:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DEVICE_NUMBER;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_DEVICE1_CLOSE:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DEVICE_NUMBER;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_READ_DEVICE2:
		if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=6)
		{
			memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
			user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
			printf("FUSHI_READ_DEVICE2 is %d \r\n",user_recvbuff_tmp[3]);
			user_fushi_status.send_status=FUSHI_READ_DATA;//read data
			user_send_times=0;
			return 0;
		}
		break;
		case FUSHI_SET_DEVICE2_OPEN:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DEVICE_NUMBER;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_DEVICE2_CLOSE:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DEVICE_NUMBER;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_DEVICE_RESET:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=5)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DATA;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_DEVICE_TIME:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=5)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DATA;//read data
				user_send_times=0;
				return 0;
			}
		break;
		case FUSHI_SET_CHECK_TIME:
			if(user_uart_serial_get_datalength(&user_drive_serial_handle)>=8)
			{
				memset(user_recvbuff_tmp,0,sizeof(user_recvbuff_tmp));
				user_uart_serial_get_data(&user_drive_serial_handle,user_recvbuff_tmp,user_uart_serial_get_datalength(&user_drive_serial_handle));
				user_fushi_status.send_status=FUSHI_READ_DATA;//read data
				user_send_times=0;
				return 0;
			}
		break;
		
	}
	
	if(user_send_times>3)
	{
		user_fushi_drive_init();
		return 3;
	}
	
	return 0xff;
}
uint8_t  user_fushi_drive_check_num(void)
{
	user_send_fushi_times_num_1000ms++;	
	if(user_fushi_status.send_status==FUSHI_READ_DATA)
	{
		
		if(user_send_fushi_times_num_1000ms>USER_SEND_INV_TIMES)
		{	
			user_send_times++;
			user_send_fushi_times_num_1000ms=0;
			return 1;
		}
		else
		{
			return 0;
		}

	}
	else
	{
		if(user_send_fushi_times_num_1000ms>8)//8S
		{	
			user_send_times++;
			user_send_fushi_times_num_1000ms=0;
			return 1;
		}
		else
		{
			return 0;
		}
	}

}


